<?php
namespace app\admin\controller;

use think\Cookie;
use think\Request;

class Tools extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }

    public function createModel(Request $request){
        if($request->post()){
            $admin_id = Cookie::get('admin_id');
            if($admin_id != 1){
                return $this->ajaxResult(3);
            }
            $modelName = $request->post('modelName');
            $isFile = $this->checkModelFile($modelName);
            if($isFile == false){
                file_put_contents("../application/admin/model/".$modelName.".php",
                    "<?php\r\nnamespace app\admin\model;\r\n\r\nuse think\Model;
                    \r\n\r\nclass ".$modelName." extends Model
                    \r\n{
                    \r\n    public function getAll(\$where){
                    \r\n        \$rs = ".$modelName."::where(\$where)
                    \r\n        ->select();
                    \r\n        return \$rs;
                    \r\n    }
                    \r\n    public function getOne(\$where){
                    \r\n        \$rs = ".$modelName."::where(\$where)
                    \r\n        ->find();
                    \r\n        return \$rs;
                    \r\n    }
                    \r\n    public function add(\$data){
                    \r\n        \$rs = ".$modelName."::data(\$data)
                    \r\n        ->save();
                    \r\n        return \$rs;
                    \r\n    }
                    \r\n    public function edit(\$where,\$data){
                    \r\n        \$rs = ".$modelName."::where(\$where)
                    \r\n        ->update(\$data);
                    \r\n        return \$rs;
                    \r\n    }
                    \r\n    public function del(\$where){
                    \r\n        \$rs = ".$modelName."::where(\$where)
                    \r\n        ->delete();
                    \r\n        return \$rs;
                    \r\n    }
                    \r\n}
                    \r\n");//这里说一下\r\n在双引号下
                return $this->ajaxResult(1);
            }
            return $this->ajaxResult(0);
        }
        return $this->fetch();
    }

}